package cn.acewill.yxd.yunxundian.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.baidu.mapapi.model.LatLng;
import com.baidu.mapapi.utils.DistanceUtil;


import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

import cn.acewill.yxd.yunxundian.R;
import cn.acewill.yxd.yunxundian.model.Shop;
import cn.acewill.yxd.yunxundian.ui.sign.SignMainActivity;

/**
 * Created by AAA on 2017/4/18.
 */

public class SignStoreAdapter extends BaseAdapter{

    private goSignClick goSignClick;
    private Context context;
    private List<Shop> list;
    private LatLng myPositon=SignMainActivity.MyPosition;
    public SignStoreAdapter(Context context,List<Shop> list,goSignClick goSignClick){
        this.context=context;
        this.list=list;
        this.goSignClick=goSignClick;
    }

    @Override
    public int getCount() {
        return list==null?0:list.size();
    }

    @Override
    public Object getItem(int position) {
        return list.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        final ViewHolder viewHolder;
        if(convertView==null){
            convertView = LayoutInflater.from(context).inflate(R.layout.sign_store_list,parent,false);
            viewHolder=new ViewHolder();
            viewHolder.storeNum=(TextView) convertView.findViewById(R.id.storeNum);
            viewHolder.storeName=(TextView) convertView.findViewById(R.id.storeName);
            viewHolder.distance=(TextView) convertView.findViewById(R.id.distance);
            viewHolder.address=(TextView) convertView.findViewById(R.id.address);
            viewHolder.goSign= (ImageView) convertView.findViewById(R.id.goSign);
            convertView.setTag(viewHolder);
        }else{
            viewHolder=(ViewHolder)convertView.getTag();
        }
        final Shop bean=list.get(position);
        viewHolder.storeNum.setText(bean.getShopNum());
        viewHolder.storeName.setText(bean.getShopName());
        viewHolder.address.setText(bean.getAddress());
        viewHolder.goSign.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Shop bean=list.get(position);
                goSignClick.goToSign(bean);

            }
        });
        double lot=bean.getxCol();
        double lat=bean.getyCol();
        LatLng lp=new LatLng(lat,lot);
        double distance= DistanceUtil.getDistance(lp,myPositon);
        int dist=(int)distance;
        if(dist>=1000){
            double dd=(double) dist/1000;
            double bb=Math.round(dd*10)/10.0;
            viewHolder.distance.setText(bb+"km");
        }else{
            viewHolder.distance.setText(dist+"m");
        }
        return convertView;

    }

    class ViewHolder {
        TextView storeNum;
        TextView storeName;
        TextView distance;
        TextView address;
        ImageView goSign;
    }

    public interface goSignClick{
        void goToSign(Shop bean);
    }
}
